package com.scisoft.scan.control.pack;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.scisoft.scan.control.ClearEditText;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.control.MainMenuActivity;
import com.scisoft.scan.data.pack.PackInfoBean;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.WebServiceHelper_Pack;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class PackInfoActivity extends Activity {
    private ImageView iv_Back; //返回按钮
    private ImageView iv_query; //搜索按钮
    private TextView bt_packed;
    private TextView bt_packing;
    private TextView bt_unpack;
    private SwipeRefreshLayout mRefreshLayout;
    private ListView lv_data;
    private View v_packed;
    private View v_packing;
    private View v_unpack;
    private ClearEditText et_bar;
    //全部的信息
    private List<PackInfoBean> packInfoBeanList;
    //当前过滤后的订单，列表操作的是这个
    private List<PackInfoBean> filterDepotOrderBeanList;
    private String flag;//1 未打包 2 打包中 3已打包

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//即隐藏标题栏
        setContentView(R.layout.activity_pack_info);

        //下拉刷新控件
        mRefreshLayout = findViewById(R.id.swipe_layout);
        mRefreshLayout.setProgressViewEndTarget(true,0);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRefreshLayout.setRefreshing(false);
                CommonUtil.showPress(PackInfoActivity.this);
                QueryPackInfoTask QueryPackInfoTask = new QueryPackInfoTask(PackInfoActivity.this);
                QueryPackInfoTask.execute();
            }
        });
        //数据列表控件
        lv_data = findViewById(R.id.lv_data);
        lv_data.setOnItemClickListener(new ListViewClick());
        bt_packed = findViewById(R.id.bt_packed);
        bt_packing = findViewById(R.id.bt_packing);
        bt_unpack = findViewById(R.id.bt_unpack);
        v_packed = findViewById(R.id.v_pack);
        v_packing = findViewById(R.id.v_packing);
        v_unpack = findViewById(R.id.v_unpack);
        et_bar = findViewById(R.id.et_bar);
        et_bar.setText("");
        //页签切换
        bt_packed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
            }
        });
        bt_packing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
            }
        });
        bt_unpack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
            }
        });
        setButton(bt_unpack);

        //返回
        iv_Back = findViewById(R.id.iv_back);
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PackInfoActivity.this, MainMenuActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

            }
        });
        //搜索框
        iv_query = findViewById(R.id.iv_query);
        iv_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                filterData(et_bar.getUpperText().toString());
                BindData(PackInfoActivity.this);
            }
        });
    }

    //设置tab页的颜色
    private void setButton(View v) {
        if (v.getId() == bt_packed.getId()) {
            flag = "3";//已打包
            v_packed.setVisibility(View.VISIBLE);
            v_packing.setVisibility(View.INVISIBLE);
            v_unpack.setVisibility(View.INVISIBLE);
            bt_packed.setTextColor(getResources().getColor(R.color.tab_text_color_select));
            bt_packing.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_unpack.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
        } else if (v.getId() == bt_packing.getId()) {
            flag = "2";//打包中
            v_packed.setVisibility(View.INVISIBLE);
            v_packing.setVisibility(View.VISIBLE);
            v_unpack.setVisibility(View.INVISIBLE);
            bt_packed.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_packing.setTextColor(getResources().getColor(R.color.tab_text_color_select));
            bt_unpack.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
        } else {
            flag = "1";//未打包
            v_packed.setVisibility(View.INVISIBLE);
            v_packing.setVisibility(View.INVISIBLE);
            v_unpack.setVisibility(View.VISIBLE);
            bt_packed.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_packing.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_unpack.setTextColor(getResources().getColor(R.color.tab_text_color_select));
        }
        CommonUtil.showPress(this);
        QueryPackInfoTask QueryPackInfoTask = new QueryPackInfoTask(PackInfoActivity.this);
        QueryPackInfoTask.execute();
    }

    //显示详细信息
    private class ListViewClick implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (position >= 0 && position < filterDepotOrderBeanList.size()) {
                PackInfoBean packInfoBean = filterDepotOrderBeanList.get(position);
                if (flag.equals("3")) {
                    //已打包
                    Intent intent = new Intent(PackInfoActivity.this, PackOkDetailActivity.class);
                    intent.putExtra("data", packInfoBean);
                    intent.putExtra("flag",flag);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(PackInfoActivity.this, PackDetailActivity.class);
                    intent.putExtra("data", packInfoBean);
                    intent.putExtra("flag",flag);
                    startActivity(intent);
                }

            }
        }
    }

    //过滤(配件列表包含过滤条件)
    private void filterData(String filter) {
        filterDepotOrderBeanList = new ArrayList<>();
        if (filter != null && filter != "") {
            if (packInfoBeanList != null) {
                for (int i = 0; i < packInfoBeanList.size(); i++) {
                    PackInfoBean bean = packInfoBeanList.get(i);
                    if (bean.orderNo != null && bean.orderNo.contains(filter)) {
                        filterDepotOrderBeanList.add(bean);
                    } else if (bean.packId != null && bean.packId.contains(filter)) {
                        filterDepotOrderBeanList.add(bean);
                    } else if (bean.depotName != null && bean.depotName.contains(filter)) {
                        filterDepotOrderBeanList.add(bean);
                    }else if (bean.customerName != null && bean.customerName.contains(filter)) {
                        filterDepotOrderBeanList.add(bean);
                    }
                }
            }
        } else {
            if (packInfoBeanList != null) {
                for (int i = 0; i < packInfoBeanList.size(); i++) {
                    PackInfoBean bean = packInfoBeanList.get(i);
                    filterDepotOrderBeanList.add(bean);
                }
            }
        }
    }

    //绑定数据
    private void BindData(Context context) {
        PackInfoAdapter adapter = new PackInfoAdapter(filterDepotOrderBeanList, context);
        lv_data.setAdapter(adapter);
    }

    //查询数据
    class QueryPackInfoTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        QueryPackInfoTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                packInfoBeanList = WebServiceHelper_Pack.PACK_01(flag);
                msg = "成功";

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String msg) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                filterData(et_bar.getUpperText().toString());
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }


}
